home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / show / mpeg2decodeWOS.lha / mpeg2decode / src / getbits.c < prev    next >
C/C++ Source or Header  |  1999-02-23  |  5KB  |  215 lines

  1. /* getbits.c, bit level routines                                            */
  2.  
  3. /*
  4.  * All modifications (mpeg2decode -> mpeg2play) are
  5.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  6.  */
  7.  
  8. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  9.  
  10. /*
  11.  * Disclaimer of Warranty
  12.  *
  13.  * These software programs are available to the user without any license fee or
  14.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  15.  * any and all warranties, whether express, implied, or statuary, including any
  16.  * implied warranties or merchantability or of fitness for a particular
  17.  * purpose.  In no event shall the copyright-holder be liable for any
  18.  * incidental, punitive, or consequential damages of any kind whatsoever
  19.  * arising from the use of these programs.
  20.  *
  21.  * This disclaimer of warranty extends to the user of these programs and user's
  22.  * customers, employees, agents, transferees, successors, and assigns.
  23.  *
  24.  * The MPEG Software Simulation Group does not represent or warrant that the
  25.  * programs furnished hereunder are free of infringement of any third-party
  26.  * patents.
  27.  *
  28.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  29.  * are subject to royalty fees to patent holders.  Many of these patents are
  30.  * general enough such that they are unavoidable regardless of implementation
  31.  * design.
  32.  *
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include "config.h"
  39. #include "global.h"
  40.  
  41. /* initialize buffer, call once before first getbits or showbits */
  42.  
  43. void Initialize_Buffer()
  44. {
  45.   ld->Incnt = 0;
  46.   ld->Rdptr = ld->Rdbfr + 2048;
  47.   ld->Rdmax = ld->Rdptr;
  48.  
  49. #ifdef VERIFY
  50.   /*  only the verifier uses this particular bit counter 
  51.    *  Bitcnt keeps track of the current parser position with respect
  52.    *  to the video elementary stream being decoded, regardless 
  53.    *  of whether or not it is wrapped within a systems layer stream 
  54.    */
  55.   ld->Bitcnt = 0;
  56. #endif
  57.  
  58.   ld->Bfr = 0;
  59.   Flush_Buffer(0); /* fills valid data into bfr */
  60. }
  61.  
  62. void Fill_Buffer()
  63. {
  64.   int Buffer_Level;
  65.  
  66.   Buffer_Level = fread(ld->Rdbfr,1,2048,ld->Infile);
  67.   ld->Rdptr = ld->Rdbfr;
  68.  
  69.   if (System_Stream_Flag)
  70.     ld->Rdmax -= 2048;
  71.  
  72.   
  73.   /* end of the bitstream file */
  74.   if (Buffer_Level < 2048)
  75.   {
  76.     /* just to be safe */
  77.     if (Buffer_Level < 0)
  78.       Buffer_Level = 0;
  79.  
  80.     /* pad until the next to the next 32-bit word boundary */
  81.     while (Buffer_Level & 3)
  82.       ld->Rdbfr[Buffer_Level++] = 0;
  83.  
  84.     /* pad the buffer with sequence end codes */
  85.     while (Buffer_Level < 2048)
  86.     {
  87.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
  88.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
  89.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
  90.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
  91.     }
  92.   }
  93. }
  94.  
  95.  
  96. /* MPEG-1 system layer demultiplexer */
  97.  
  98. int Get_Byte()
  99. {
  100.   while(ld->Rdptr >= ld->Rdbfr+2048)
  101.   {
  102.     fread(ld->Rdbfr,1,2048,ld->Infile);
  103.     ld->Rdptr -= 2048;
  104.     ld->Rdmax -= 2048;
  105.   }
  106.   return *ld->Rdptr++;
  107. }
  108.  
  109. /* extract a 16-bit word from the bitstream buffer */
  110. int Get_Word()
  111. {
  112.   int Val;
  113.  
  114.   Val = Get_Byte();
  115.   return (Val<<8) | Get_Byte();
  116. }
  117.  
  118.  
  119. /* return next n bits (right adjusted) without advancing */
  120.  
  121. #ifdef STORM
  122. unsigned int Show_Bits(int N)
  123. #else
  124. unsigned int Show_Bits(N)
  125. int N;
  126. #endif
  127. {
  128.   return ld->Bfr >> (32-N);
  129. }
  130.  
  131.  
  132. /* return next bit (could be made faster than Get_Bits(1)) */
  133.  
  134. unsigned int Get_Bits1()
  135. {
  136.   return Get_Bits(1);
  137. }
  138.  
  139.  
  140. /* advance by n bits */
  141.  
  142. #ifdef STORM
  143. void Flush_Buffer(int N)
  144. #else
  145. void Flush_Buffer(N)
  146. int N;
  147. #endif
  148. {
  149.   int Incnt;
  150.  
  151.   ld->Bfr <<= N;
  152.  
  153.   Incnt = ld->Incnt -= N;
  154.  
  155.   if (Incnt <= 24)
  156.   {
  157.     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  158.     {
  159.       do
  160.       {
  161.         if (ld->Rdptr >= ld->Rdmax)
  162.           Next_Packet();
  163.         ld->Bfr |= Get_Byte() << (24 - Incnt);
  164.         Incnt += 8;
  165.       }
  166.       while (Incnt <= 24);
  167.     }
  168.     else if (ld->Rdptr < ld->Rdbfr+2044)
  169.     {
  170.       do
  171.       {
  172.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  173.         Incnt += 8;
  174.       }
  175.       while (Incnt <= 24);
  176.     }
  177.     else
  178.     {
  179.       do
  180.       {
  181.         if (ld->Rdptr >= ld->Rdbfr+2048)
  182.           Fill_Buffer();
  183.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  184.         Incnt += 8;
  185.       }
  186.       while (Incnt <= 24);
  187.     }
  188.     ld->Incnt = Incnt;
  189.   }
  190.  
  191. #ifdef VERIFY 
  192.   ld->Bitcnt += N;
  193. #endif /* VERIFY */
  194.  
  195. }
  196.  
  197.  
  198. /* return next n bits (right adjusted) */
  199.  
  200. #ifdef STORM
  201. unsigned int Get_Bits(int N)
  202. #else
  203. unsigned int Get_Bits(N)
  204. int N;
  205. #endif
  206. {
  207.   unsigned int Val;
  208.  
  209.   Val = Show_Bits(N);
  210.   Flush_Buffer(N);
  211.  
  212.   return Val;
  213. }
  214.  
  215.